#!/usr/bin/python
# -*- coding: utf-8 -*-
# @name             : 获取时间
# @author           : OneYoung
# @create           : 2024/02/04
# @version          : v1.0
# @desc             : 封装常用的时间获取信息，避免重复造车

import time
import datetime


def except_str_datetime(f) -> str:
    """
    返回预期格式的字符串日期时间
    :param f: 字符串格式，如：%Y-%m-%d
    """
    return time.strftime(f)


def datetime_to_str(date_time) -> str:
    """
    日期类型转字符串日期时间
    :param date_time: 需要转换的日期数据
    """
    return date_time.strftime('%Y-%m-%d %H:%M:%S')


def str_date() -> str:
    """当前日期：%Y-%m-%d"""
    f = '%Y-%m-%d'
    return time.strftime(f)


def str_time() -> str:
    """当前时间：%H:%M:%S"""
    f = '%H:%M:%S'
    return time.strftime(f)


def str_datetime() -> str:
    """当前日期时间：%Y-%m-%d %H:%M:%S"""
    f = '%Y-%m-%d %H:%M:%S'
    return time.strftime(f)


def str_full_datetime() -> str:
    """当前日期时间：%Y%m%d%H%M%S"""
    f = '%Y%m%d%H%M%S'
    return time.strftime(f)


def date_today():
    """返回当前日期"""
    return datetime.date.today()


def datetime_now():
    """返回当前时间"""
    return datetime.datetime.now()


def timestamp_to_str(timestamp):
    """
    时间戳转字符串日期时间
    :param timestamp: 时间戳
    """
    if len(str(timestamp)) > 10:
        timestamp = int(str(timestamp)[:10])
    return datetime.datetime.fromtimestamp(timestamp)


def str_to_timestamp(string, millisecond=False):
    """字符串日期时间转时间戳
    :param string: 格式: %Y-%m-%d %H:%M:%S
    :param millisecond: 是否返回13位时间戳,默认10位
    """
    time_tuple = time.strptime(string, "%Y-%m-%d %H:%M:%S")
    stamp = time.mktime(time_tuple)
    timestamp = int(stamp)
    if millisecond:
        timestamp = int(stamp * 1000)
    return timestamp


def timestamp_now(millisecond=False):
    """
    返回当前时间戳
    :param millisecond: 是否返回13位时间戳,默认10位
    """
    dt = datetime.datetime.now()
    stamp = datetime.datetime.timestamp(dt)
    timestamp = int(stamp)
    if millisecond:
        timestamp = int(stamp * 1000)
    return timestamp


def offset_str_datetime(**delta):
    """获取带偏移量的日期时间
    :param delta: weeks/days/hours/minutes/seconds/milliseconds/microseconds
    """
    offset = datetime.timedelta(**delta)
    except_time = datetime_now() + offset
    return datetime_to_str(except_time)


def offset_timestamp(millisecond=False, **delta):
    """获取带偏移量的时间戳
    :param millisecond: 返回13位时间戳,默认10位
    :param delta: weeks/days/hours/minutes/seconds/milliseconds/microseconds
    """
    offset = offset_str_datetime(**delta)
    return str_to_timestamp(string=offset, millisecond=millisecond)
